﻿Imports System.Text
Imports DotNetNuke.Modules.GeminorumSoftwareContacts

<TestClass()>
Public Class SqlDataProviderHelperTests
    Private ReadOnly Conn As New GeminorumSoftware.Database.ConnectionString("dotnetnuke", "dotnetnuke", "DotNetNukeGeminorum", ".", "Unit Tester")
    Private ReadOnly DefaultTestContactModuleID As Integer = 99

    <TestInitialize()>
    Public Sub Init()
        Dim scriptExecuter As New ContactsScriptExecuter
        scriptExecuter.ExecuteCreateScript()
    End Sub

#Region "---TestAddContact---"
    <TestMethod()>
    Public Sub TestAddContact()
        Dim target As New SqlDataProviderHelper(Conn.ToString)
        Dim testContact As Contact = CreateNewContactInstance()
        Dim id As Integer = target.AddContact(testContact)
        Dim loadedContact As Contact = LoadContact(target.GetContact(id))

        Assert.AreEqual(testContact.CellPhoneNumber, loadedContact.CellPhoneNumber)
        Assert.AreEqual(testContact.ContentItemId, loadedContact.ContentItemId)
        Assert.AreEqual(testContact.Email, loadedContact.Email)
        Assert.AreEqual(testContact.Name, loadedContact.Name)
        Assert.AreEqual(testContact.PhoneNumber, loadedContact.PhoneNumber)
        Assert.AreEqual(testContact.Title, loadedContact.Title)
        Assert.AreNotEqual(0, loadedContact.ID)
    End Sub
#End Region
#Region "---TestUpdateContact---"
    <TestMethod()>
    Public Sub TestUpdateContact()
        Dim target As New SqlDataProviderHelper(Conn.ToString)
        Dim testContact As Contact = CreateNewContactInstance()
        Dim id As Integer = target.AddContact(testContact)
        Dim id2 As Integer = target.AddContact(testContact)

        testContact.ID = id
        testContact.Name = "Name1"
        testContact.CellPhoneNumber = "CellPhone1"
        testContact.ContentItemId = 99
        testContact.Email = "Email1"
        testContact.PhoneNumber = "PhoneNumber1"
        testContact.Title = "Title1"

        target.UpdateContact(testContact)

        Dim loadedContact As Contact = LoadContact(target.GetContact(id))

        Assert.AreEqual("CellPhone1", loadedContact.CellPhoneNumber)
        Assert.AreEqual(99, loadedContact.ContentItemId)
        Assert.AreEqual("Email1", loadedContact.Email)
        Assert.AreEqual("Name1", loadedContact.Name)
        Assert.AreEqual("PhoneNumber1", loadedContact.PhoneNumber)
        Assert.AreEqual("Title1", loadedContact.Title)
        Assert.AreNotEqual(0, loadedContact.ID)


        Dim loadedContact1 As Contact = LoadContact(target.GetContact(id2))

        Assert.AreEqual("CellPhone", loadedContact1.CellPhoneNumber)
        Assert.AreEqual("Email", loadedContact1.Email)
        Assert.AreEqual("Name", loadedContact1.Name)
        Assert.AreEqual("PhoneNumber", loadedContact1.PhoneNumber)
        Assert.AreEqual("Title", loadedContact1.Title)
    End Sub
#End Region
#Region "---TestDeleteContact---"
    <TestMethod()>
    Public Sub TestDeleteContact()
        Dim target As New SqlDataProviderHelper(Conn.ToString)
        Dim testContact As Contact = CreateNewContactInstance()
        Dim id As Integer = target.AddContact(testContact)
        Dim id2 As Integer = target.AddContact(testContact)

        target.DeleteContact(id)

        Assert.AreEqual(False, target.GetContact(id).Read)
        Assert.AreEqual(True, target.GetContact(id2).Read)
    End Sub
#End Region
#Region "---TestGetContacts---"
    <TestMethod()>
    Public Sub TestGetContacts()
        Dim target As New SqlDataProviderHelper("Data Source=.;Initial Catalog=DotNetNukeGeminorum;User ID=sa;Password=LanG_Qm52")
        Dim testContact As Contact = CreateNewContactInstance()
        Dim id As Integer = target.AddContact(testContact)
        Dim id2 As Integer = target.AddContact(testContact)

        Dim reader As IDataReader = target.GetContacts(testContact.ModuleID)

        Assert.AreEqual(True, reader.Read())
        Assert.AreEqual(id, reader("ID"))
        Assert.AreEqual(True, reader.Read())
        Assert.AreEqual(id2, reader("ID"))
    End Sub
#End Region
#Region "---TestDeleteContacts---"
    <TestMethod()>
    Public Sub TestDeleteContacts()
        Dim target As New SqlDataProviderHelper(Conn.ToString)
        Dim testContact As Contact = CreateNewContactInstance()
        Dim id1 As Integer = target.AddContact(testContact)
        Dim id2 As Integer = target.AddContact(testContact)
        testContact.ModuleID = 100
        Dim id3 As Integer = target.AddContact(testContact)
        Dim id4 As Integer = target.AddContact(testContact)

        target.DeleteContacts(DefaultTestContactModuleID)

        Assert.AreEqual(False, target.GetContact(id1).Read)
        Assert.AreEqual(False, target.GetContact(id2).Read)
        Assert.AreEqual(True, target.GetContact(id3).Read)
        Assert.AreEqual(True, target.GetContact(id4).Read)
    End Sub
#End Region

#Region "---CreateNewContactInstance---"
    Private Function CreateNewContactInstance() As Contact
        Dim testContact As New Contact

        testContact.CellPhoneNumber = "CellPhone"
        testContact.ContentItemId = 44
        testContact.Email = "Email"
        testContact.ModuleID = DefaultTestContactModuleID
        testContact.Name = "Name"
        testContact.PhoneNumber = "PhoneNumber"
        testContact.Title = "Title"

        Return testContact
    End Function
#End Region
#Region "---LoadContact---"
    Private Function LoadContact(reader As IDataReader) As Contact
        Dim testContact As New Contact
        reader.Read()
        testContact.Fill(reader)
        Return testContact
    End Function
#End Region

    <TestCleanup()>
    Public Sub Cleanup()
        Dim scriptExecuter As New ContactsScriptExecuter
        scriptExecuter.ExecuteDropScript()
    End Sub
End Class